/*
 *
 *  *  Copyright (c) 2019-2020, 冷冷 (wangiegie@gmail.com).
 *  *  <p>
 *  *  Licensed under the GNU Lesser General Public License 3.0 (the "License");
 *  *  you may not use this file except in compliance with the License.
 *  *  You may obtain a copy of the License at
 *  *  <p>
 *  * https://www.gnu.org/licenses/lgpl.html
 *  *  <p>
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 *
 */
package com.base.pig.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.pig.constant.CacheConstants;
import com.base.pig.constant.enums.DictTypeEnum;
import com.base.pig.entity.SysDict;
import com.base.pig.entity.SysDictItem;
import com.base.pig.util.R;
import com.base.pig.service.SysDictService;

import com.base.pig.mapper.SysDictItemMapper;
import com.base.pig.mapper.SysDictMapper;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.Map;

/**
 * 字典表
 *
 * @author lengleng
 * @date 2019/03/19
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

	private final SysDictItemMapper dictItemMapper;

	/**
	 * 根据ID 删除字典
	 *
	 * @param id 字典ID
	 * @return
	 */
	@Override
	@CacheEvict(value = CacheConstants.DICT_DETAILS, allEntries = true)
	@Transactional(rollbackFor = Exception.class)
	public R removeDict(Long id) {
		SysDict dict = this.getById(id);
		// 系统内置
		if (DictTypeEnum.SYSTEM.getType().equals(dict.getSystemType())) {
			return R.failed(null,"系统内置字典不能删除");
		}

		baseMapper.deleteById(id);
		dictItemMapper.delete(Wrappers.<SysDictItem>lambdaQuery()
				.eq(SysDictItem::getDictId, id));
		return R.ok(null, "删除数据成功");
	}

	/**
	 * 更新字典
	 *
	 * @param dict 字典
	 * @return
	 */
	@Override
	public R updateDict(SysDict dict) {
		SysDict sysDict = this.getById(dict.getId());
		// 系统内置
		if (DictTypeEnum.SYSTEM.getType().equals(sysDict.getSystemType())) {
			return R.failed(null,"系统内置字典不能修改");
		}
		return R.ok(this.updateById(dict),"修改字典信息成功");
	}

	@Override
	public R<IPage> getDictPage(Map map) {
		//当前页
		Long current ;
		//每页显示条数，默认 10
		Long size ;
		//获取类型
		String type = String.valueOf(map.get("type"));
		//获取字典类型
		String system = String.valueOf(map.get("systemType"));

		Page<SysDict> page = new Page<>();
		QueryWrapper queryWrapper = new QueryWrapper<SysDict>();
		Iterator keys = map.keySet().iterator();
		while (keys.hasNext()){
			String key = (String)keys.next();
			if("type".equals(key)){
				if(StrUtil.isNotBlank(type)){
					queryWrapper.like("type",type);
				}
			}
			if("systemType".equals(key)){
				if(StrUtil.isNotBlank(system)){
					queryWrapper.like("system_type",system);
				}
			}
			if("current".equals(key)){
				if (StrUtil.isBlank(String.valueOf(map.get("current")))){
					current = 1L;
				}else {
					current = Long.valueOf(String.valueOf(map.get("current")));
				}
				page.setCurrent(current);
			}
			if("size".equals(key)){
				if (StrUtil.isBlank(String.valueOf(map.get("size")))){
					size = 10L;
				}else {
					size = Long.valueOf(String.valueOf(map.get("size")));
				}
				page.setSize(size);
			}
			queryWrapper.orderByDesc("create_time");
		}
		return R.ok(this.page(page,queryWrapper));
	}
}
